﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Cellula.Client.ControlHelper;
using Cn.Cellula.Model.Calendar;
using Cn.Edwon.Data;
using System.Data;
using Cn.Edwon.Web;

namespace Cn.Cellula.Website.Admin.Module.Manhour {
    public partial class List : PageSupport {
        ICalendarBiz biz = BizFactory.Get<ICalendarBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            GvHelper.AppendEventHandler(ref this.gvItem);
            if (!Page.IsPostBack) {
                InitForm();
                LoadEOList();
            }

        }
        private void FixCheckData(DateTime dt) {
            String hql = "from TCalendarMonthlyCheckEO eo where eo.CheckYear=" + dt.Year;
            hql += " and eo.CheckMonth=" + dt.Month;
            IList<TCalendarMonthlyCheckEO> eos = biz.Find<TCalendarMonthlyCheckEO>(hql);
            if (eos == null || eos.Count < 1) {
                TCalendarMonthlyCheckEO eo = new TCalendarMonthlyCheckEO();
                eo.CheckMonth = dt.Month;
                eo.CheckYear = dt.Year;
                eo.Operator = "system";
                eo.State = 0;
                biz.Save<TCalendarMonthlyCheckEO>(eo);
            }
        }
        private void InitForm() {
            FixCheckData(DateTime.Now.AddMonths(-1));
            FixCheckData(DateTime.Now);
        }
        private void LoadEOList() {
            IList<VCalendarMonthlyCheckEO> list = biz.LoadAll<VCalendarMonthlyCheckEO>();
            this.gvItem.DataSource = list;
            this.gvItem.Attributes[GvHelper.ATTR_TOTAL_COUNT] = (list != null) ? list.Count.ToString() : "0";
            this.gvItem.DataBind();
        }
        protected void gvPager_OnSelectedIndexChanged(object sender, EventArgs e) {
            DropDownList ddl = (DropDownList)sender;
            this.gvItem.PageIndex = Convert.ToInt32(ddl.SelectedValue) - 1;
            LoadEOList();
        }
        protected void gvItem_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvItem.PageIndex = e.NewPageIndex;
            LoadEOList();
        }
        protected void gvItem_RowCommand(object sender, GridViewCommandEventArgs e) {
            String pKey = e.CommandArgument.ToString();
            TCalendarMonthlyCheckEO eo = biz.Load<TCalendarMonthlyCheckEO>(pKey);
            if (e.CommandName == "CheckIn") {
                try {
                    IList<CallingParameter> ps = new List<CallingParameter>();
                    ps.Add(new CallingParameter("r_check_key", pKey, false));
                    ps.Add(new CallingParameter("r_islock", 1, false));
                    DataSet ds = biz.ADOExcuteProc("p_calendar_action_monthly_check", ps);
                } catch (Exception ex) {
                    log.Error("MonthlyCheckIn:UnCheckIn Procedure Exception:" + ex.Message);
                    SiteInfo.Send(Page, "ERROR", "锁定任务数据时错误");
                    return;
                }
                eo.State = 1;
                eo.Operator = base.Actor.ID;
                eo.LastDt = DateTime.Now;
                biz.Update<TCalendarMonthlyCheckEO>(eo);
            } else if (e.CommandName == "UnCheckIn") {
                try {
                    IList<CallingParameter> ps = new List<CallingParameter>();
                    ps.Add(new CallingParameter("r_check_key", pKey, false));
                    ps.Add(new CallingParameter("r_islock", 0, false));
                    DataSet ds = biz.ADOExcuteProc("p_calendar_action_monthly_check", ps);
                } catch (Exception ex) {
                    log.Error("MonthlyCheckIn:UnCheckIn Procedure Exception:" + ex.Message);
                    SiteInfo.Send(Page, "ERROR", "解锁任务数据时错误");
                    return;
                }
                eo.State = 0;
                eo.Operator = base.Actor.ID;
                eo.LastDt = DateTime.Now;
                biz.Update<TCalendarMonthlyCheckEO>(eo);
            }
            this.LoadEOList();
        }
    }
}